package org.hoyi.util;

import java.util.HashMap;
import java.util.Map;


/**
 *          Author:ellen
 *          Email:ellen@kuaifish.com
 *          CreateDate:2015-05-20
 *          ModifyDate:2015-06-20
 *          hoyi entities @ hoyi.org
 *          使用请在项目关于内标注hoyi版权，
 *          hoyi版权归hoyi.org所有
 */
public class ConStr
{
  public String Pre;
  public String Start;
  public String StartToEnd;
  public String End;
  public String EndEx;
  public String Content;
  public Map<Integer, String> mps;
  public Map<Integer, Integer> lengthmap;

  public ConStr()
  {
  }

  public String toString()
  {
    return "PRE:" + this.Pre + "\n" + 
      "Start:" + this.Start + "\n" + 
      "StartToEnd:" + this.StartToEnd + "\n" + 
      "End:" + this.End + "\n" + 
      "EndEx:" + this.EndEx + "\n";
  }

  public String ReplaceKey(String _content, String key, String value)
  {
    _content = _content.replace(key, value);
    return _content;
  }

  public void CalKeys(String _content)
  {
    this.mps = new HashMap();
    this.lengthmap = new HashMap();
    String tmp = _content;

    int startindex = _content.indexOf("{#");
    int endindex = _content.indexOf("#}", startindex);
    if ((startindex > 0) && (endindex > 0)) {
      this.mps.put(Integer.valueOf(startindex), _content.substring(startindex, endindex + 2));
      this.lengthmap.put(Integer.valueOf(startindex), Integer.valueOf(endindex - startindex + 2));

      while ((_content.indexOf("{#", endindex) > 0) && (_content.indexOf("#}", endindex + 2) > 0)) {
        startindex = _content.indexOf("{#", endindex);
        endindex = _content.indexOf("#}", endindex + 2);

        if ((startindex > 0) && (endindex > 0)) {
          this.mps.put(Integer.valueOf(startindex), _content.substring(startindex, endindex + 2));
          this.lengthmap.put(Integer.valueOf(startindex), Integer.valueOf(endindex - startindex + 2));
        }
      }
    }
  }

  public ConStr(String _content, String _start, String _end)
  {
    if ((_content != null) && (_content.length() > 0)) {
      this.Content = _content;
      this.Start = _start;
      this.End = _end;

      int startindex = this.Content.indexOf(this.Start);
      int endindex = this.Content.indexOf(this.End, startindex);
      if ((startindex > 0) && (endindex > 0)) {
        this.Pre = this.Content.substring(0, startindex);
        if (endindex > 0)
        {
           this.StartToEnd = this.Content.substring(startindex + this.Start.length(), endindex);

           this.EndEx = this.Content.substring(endindex + this.End.length(), this.Content.length());
        }
        else
        {
           System.out.println("Error for Split Str 1");
        }
      }
      else {
         System.out.println("Error for Split Str 2");
      }
    }
  }
}
